package com.hua.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import org.springframework.stereotype.Component;

@Component
public class sendPhone {

    public boolean send( Map<String,Object> param,String phone) {
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4GHGT6d2t7VA86sDoq56", "fa1CM2G2KoRKqtLikLxwuETiaDgx3o");
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/

        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName("生活中的智慧");//申请阿里云的签名名称
        request.setPhoneNumbers(phone);// 手机号
        request.setTemplateCode("SMS_213086464");//申请阿里云  模板code
        request.setTemplateParam(JSONObject.toJSONString(param)); //验证码数据，装换成json数据传递
        //最终发送
        try{
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            System.out.println(response.getMessage());
            if (response.getMessage().equals("OK")){
                return true;
            }else {
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }
}
